﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;


public partial class User_save : System.Web.UI.Page
{
    static string id;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request["ID"];
        string value = "";

        if (!IsPostBack)
        {
            string str = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            //部门下拉菜单
            string cmdtext = "select * from tb_Department";
            SqlCommand cmd = new SqlCommand(cmdtext, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            DropDownListDepartment.DataSource = dt;
            DropDownListDepartment.DataTextField = "Department";
            DropDownListDepartment.DataValueField = "ID";
            DropDownListDepartment.DataBind();

            //职务下拉菜单
            string cmdtext1 = "select * from tb_Position";
            SqlCommand cmd1 = new SqlCommand(cmdtext1, conn);
            adapter = new SqlDataAdapter(cmd1);
            dt = new DataTable();
            adapter.Fill(dt);
            DropDownListPosition.DataSource = dt;
            DropDownListPosition.DataTextField = "Position";
            DropDownListPosition.DataValueField = "ID";
            DropDownListPosition.DataBind();

            //权限下拉菜单
            string cmdtextpermissions = "select * from tb_Permissions";
            SqlCommand cmdpermissions = new SqlCommand(cmdtextpermissions, conn);
            adapter = new SqlDataAdapter(cmdpermissions);
            dt = new DataTable();
            adapter.Fill(dt);
            dt.Rows.RemoveAt(0);
            DropDownListPermissions.DataSource = dt;
            DropDownListPermissions.DataTextField = "Permissions";
            DropDownListPermissions.DataValueField = "ID";
            DropDownListPermissions.DataBind();

            //第一学位下拉菜单
            string cmdtext2 = "select * from tb_Degree";
            SqlCommand cmd2 = new SqlCommand(cmdtext2, conn);
            adapter = new SqlDataAdapter(cmd2);
            dt = new DataTable();
            adapter.Fill(dt);
            DropDownListFirstDegree.DataSource = dt;
            DropDownListFirstDegree.DataTextField = "Degree";
            DropDownListFirstDegree.DataValueField = "ID";
            DropDownListFirstDegree.DataBind();


            //第二学位下拉菜单
            string cmdtext3 = "select * from tb_Degree";
            SqlCommand cmd3 = new SqlCommand(cmdtext3, conn);
            adapter = new SqlDataAdapter(cmd3);
            dt = new DataTable();
            adapter.Fill(dt);
            DropDownListSecondDegree.DataSource = dt;
            DropDownListSecondDegree.DataTextField = "Degree";
            DropDownListSecondDegree.DataValueField = "ID";
            DropDownListSecondDegree.DataBind();


            conn.Close();




            //判断是否有要显示的值的传递


            if (String.IsNullOrEmpty(id))
                return;

            EmployeeInfo employeeInfo = EmployeeInfo.Getworker(Int32.Parse(id));

            TextBoxName.Text = employeeInfo.Name;                                //姓名
            DropDownListDepartment.Text = getPartmentOrPosition(id, 0, ref value);                  //部门
            DropDownListDepartment.SelectedValue = value;
            //Position.Text = employeeInfo.Position;

            switch (getPermissions(id))
            {
                case "2":
                    DropDownListPermissions.Text = "人事行政主管";
                    DropDownListPermissions.SelectedValue = "2";
                    break;
                case "3":
                    DropDownListPermissions.Text = "物业经理";
                    DropDownListPermissions.SelectedValue = "3";
                    break;
                case "4":
                    DropDownListPermissions.Text = "物业中心经理";
                    DropDownListPermissions.SelectedValue = "4";
                    break;
                case "5":
                    DropDownListPermissions.Text = "部门主管";
                    DropDownListPermissions.SelectedValue = "5";
                    break;
                case "6":
                    DropDownListPermissions.Text = "员工";
                    DropDownListPermissions.SelectedValue = "6";
                    break;
            }


            //Position.Text = " SELECT  tb_Position.Position FROM tb_Position inner join tb_EmployeeInfo on tb_Position.ID=tb_EmployeeInfo.Position ";  //职务
            DropDownListPosition.Text = getPartmentOrPosition(id, 1, ref value);
            DropDownListPosition.SelectedValue = value;

            if (employeeInfo.Sex == "0")                              //性别
                RadioButtonSex.SelectedIndex = 0;
            else
                RadioButtonSex.SelectedIndex = 1;                                    //性别                 


            TextBoxBirthday.Text = employeeInfo.BirthdayYang;                //出生日期


            if (employeeInfo.BloodType == "1")                            //血型
                DropDownListBloodType.Text = "A";
            else
                if (employeeInfo.BloodType == "2")
                    DropDownListBloodType.Text = "B";
                else
                    if (employeeInfo.BloodType == "3")
                        DropDownListBloodType.Text = "AB";
                    else
                        DropDownListBloodType.Text = "O";                        //血型

            TextBoxNation.Text = employeeInfo.Nation;                            //民族
            TextBoxOrigin.Text = employeeInfo.Origin;                            //籍贯
            TextBoxPolitical.Text = employeeInfo.Political;                      //政治面貌
            TextBoxGraduationTime.Text = employeeInfo.GraduationTime;            //毕业时间
            TextBoxGraduationSchool.Text = employeeInfo.GraduationSchool;        //毕业院校
            TextBoxEdu1.Text = employeeInfo.Edu1;                                //第一学历
            TextBoxProfessional.Text = employeeInfo.Edu1Professional;        //专业
            //DropDownListFirstDegree.Text = employeeInfo.Edu1Degree;                    //学位
            TextBoxEdu2.Text = employeeInfo.Edu2;                                //第二学历
            TextBoxEdu2Professional.Text = employeeInfo.Edu2Professional;                        //专业
           // DropDownListSecondDegree.Text = employeeInfo.Edu2Degree;                        //学位
            TextBoxForLanLev.Text = employeeInfo.ForLanLev;                        //外语水平
            TextBoxComLev.Text = employeeInfo.ComLev;                        //计算机水平
            TextBoxIDCard.Text = employeeInfo.IDCard;                        //身份证号
            TextBoxPerReside.Text = employeeInfo.PerReside;                        //户口所在地
            TextBoxMarrige.Text = employeeInfo.Marrige;                        //婚姻状况
            TextBoxWorkExper.Text = employeeInfo.WorkExper;                        //工作年限
            TextBoxHealth.Text = employeeInfo.Health;                        //健康状况
            TextBoxAddress.Text = employeeInfo.Address;                        //详细住址
            TextBoxOtherAddress.Text = employeeInfo.OtherAddress;                        //其他住址
            TextBoxPhone.Text = employeeInfo.Phone;                        //座机
            TextBoxTelePhone.Text = employeeInfo.TelePhone;                        //手机
            TextBoxEmail.Text = employeeInfo.EMail;                        //E-mail
            TextBoxQQ.Text = employeeInfo.QQ;                        //QQ
            TextBoxEmerPer.Text = employeeInfo.EmerPer;                        //紧急时刻联系人
            TextBoxEmerPhone.Text = employeeInfo.EmerPhone;                        //紧急时刻联系电话
            TextBoxWorkTime.Text = employeeInfo.WorkTime;                        //参加工作时间
            TextBoxPostTime.Text = employeeInfo.PostTime;                        //正式上岗时间
            TextBoxRec.Text = employeeInfo.Rec;                        //是否有公司员工推荐
            TextBoxRecName.Text = employeeInfo.RecName;                        //推荐人
            TextBoxFam.Text = employeeInfo.Fam;                        //公司内部是否有家属
            TextBoxFamName.Text = employeeInfo.FamName;                        //家属姓名
            TextBoxDanwei.Text = employeeInfo.danwei;                        //单位                                    没有
            TextBoxFamPosition.Text = employeeInfo.FamPosition;                        //职务
            TextBoxExperience.Text = employeeInfo.Experience;                        //工作经历
            TextBoxHonors.Text = employeeInfo.Honors;                        //所获荣誉
            TextBoxSocialRelation.Text = employeeInfo.SocialRelation;                        //社会关系
            TextBoxInAndHo.Text = employeeInfo.InAndHo;                        //兴趣特长
            TextBoxSelfAsse.Text = employeeInfo.SelfAsse;                        //自我评价

            if (!String.IsNullOrEmpty(employeeInfo.Pic))
            {
                String path = ("Pic/" + employeeInfo.Pic);
                Image1.ImageUrl = path;
            }
            else
                Image1.ImageUrl = "";
        }
    }



    private string getPartmentOrPosition(string id, int flag,ref string value)
    {
        string connstring = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(connstring);
        string selentCmdStr;

        if (flag == 0)
        {
            selentCmdStr = "SELECT Department,ID FROM tb_Department WHERE ID=(SELECT tb_EmployeeInfo.Department FROM tb_EmployeeInfo WHERE ID='" + id + "')";
        }
        else
        {
            selentCmdStr = "SELECT Position,ID FROM tb_Position WHERE ID=(SELECT tb_EmployeeInfo.Position FROM tb_EmployeeInfo WHERE ID='" + id + "')";
        }

        SqlCommand selectCmd = new SqlCommand(selentCmdStr, conn);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = selectCmd;

        DataSet ds = new DataSet();
        da.Fill(ds);

        string result = ds.Tables[0].Rows[0][0].ToString();
        value = ds.Tables[0].Rows[0][1].ToString();

        return result;
    }


    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        string connstring = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(connstring);
        conn.Open();
         string cmdupdate = "update  tb_EmployeeInfo set Department ='" + DropDownListDepartment.SelectedValue + "',Position = '" + DropDownListPosition.SelectedValue +
            "',Name = '" + TextBoxName.Text + "',sex = '" + RadioButtonSex.SelectedValue + "',BirthdayYang = '" + TextBoxBirthday.Text + "',BloodType = '" + DropDownListBloodType.SelectedValue +
            "',Nation ='" + TextBoxNation.Text + "',Origin = '" + TextBoxOrigin.Text +
            "',Political ='" + TextBoxPolitical.Text + "',GraduationTime ='" + TextBoxGraduationTime.Text + "',GraduationSchool = '" + TextBoxGraduationSchool.Text + "',Edu1 ='" + TextBoxEdu1.Text +
            "',Edu1Professional = '" + TextBoxProfessional.Text + "',Edu1Degree = '" + DropDownListFirstDegree.SelectedValue + "',Edu2 = '" + TextBoxEdu2.Text + "',Edu2Professional = '" + TextBoxEdu2Professional.Text +
            "',Edu2Degree = '" + DropDownListSecondDegree.SelectedValue + "',ForLanLev = '" + TextBoxForLanLev.Text + "',ComLev = '" + TextBoxComLev.Text + "',IDCard = '" + TextBoxIDCard.Text +
            "',PerReside = '" + TextBoxPerReside.Text + "',Marrige = '" + TextBoxMarrige.Text + "',Tittle = '" + TextBoxTittle.Text + "',Health = '" + TextBoxHealth.Text + "',LoverName = '" + TextBoxLoverName.Text
            + "',LoverBir = '" + TextBoxLoverBir.Text + "',ChiName = '" + TextBoxChiName.Text + "',ChiBir = '" + TextBoxChiBir.Text + "',WorkExper = '" + TextBoxWorkExper.Text + "',Address = '" + TextBoxAddress.Text +
            "',OtherAddress = '" + TextBoxOtherAddress.Text + "',Phone = '" + TextBoxPhone.Text + "',TelePhone = '" + TextBoxTelePhone.Text + "',Email = '" + TextBoxEmail.Text + "',QQ = '" + TextBoxQQ.Text +
            "',EmerPer = '" + TextBoxEmerPer.Text + "',EmerPhone = '" + TextBoxEmerPhone.Text + "',WorkTime = '" + TextBoxWorkTime.Text + "',PostTime = '" + TextBoxPostTime.Text + "',Rec = '" + TextBoxRec.Text +
            "',RecName = '" + TextBoxRecName.Text + "',Fam = '" + TextBoxFam.Text + "',FamName = '" + TextBoxFamName.Text + "',FamPosition = '" + TextBoxFamPosition.Text + "',Experience = '" + TextBoxExperience.Text +
            "',Honors = '" + TextBoxHonors.Text + "',SocialRelation = '" + TextBoxSocialRelation.Text + "',InAndHo = '" + TextBoxInAndHo.Text + "',SelfAsse = '" + TextBoxSelfAsse.Text + "',Wage = '" 
            + TextBoxgongzi.Text + "' where ID = '" + id + "'";
        SqlCommand cmd = new SqlCommand(cmdupdate,conn);
        cmd.ExecuteNonQuery();

        string cmdUpdateUserStr = "update tb_User set Flag='" + DropDownListPermissions.SelectedValue + "' where EID='" + id + "'";
        SqlCommand cmdUpdateUser = new SqlCommand(cmdUpdateUserStr, conn);
        cmdUpdateUser.ExecuteNonQuery();

        conn.Close();

        Response.Write("<script>alert('修改成功!');window.location.href ='EManage.aspx'</script>");
    }

    private string getPermissions(string id)
    {
        string permissions = "";
        string str = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(str);

        string cmdStr = "select Flag from tb_User where EID='" + id + "' ";

        SqlCommand comm = new SqlCommand(cmdStr, conn);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = comm;

        DataSet ds = new DataSet();
        da.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)
        {
            permissions = ds.Tables[0].Rows[0][0].ToString();
        }

        conn.Close();

        return permissions;
    }
    protected void ButtonBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("EManage.aspx");
    }
}